//
//  JDNumberInputView.swift
//  DragonWallet
//
//  Created by volantPig on 2024/6/26.
//

import UIKit

class JDNumberInputView: UIView {

    var plusActionBlock: ((Int)->())?
    var minusActionBlock: ((Int)->())?
    var showNumber: Int = 1
    
    /// 是否可以主动输入
    var isCanInput: Bool = false {
        didSet {
            self.changeInputState()
        }
    }
    /// 按钮 宽度
    var buttonWidth: CGFloat = 0
    /// 这个走剩余？
//    var inputWidth: CGFloat = 0

    /// 加号 按钮
    var plusButton: UIButton = UIButton(type: .custom)
    /// 减号 按钮
    var minusButton: UIButton = UIButton(type: .custom)
    var leftLine: UIView = UIView()
    /// 输入框 默认不可输入
    var numberInputView: UITextField = UITextField()
    var rightLine: UIView = UIView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setupUIElements()
        self.refreshConstraints()
    }
    
    func setupUIElements() {
        
        self.minusButton.setTitle("-", for: .normal)
        self.minusButton.setTitleColor(color("#333333"), for: .normal)
        self.minusButton.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .regular)
        self.minusButton.addTarget(self, action: #selector(minusAction), for: .touchUpInside)
        
        self.plusButton.setTitle("+", for: .normal)
        self.plusButton.setTitleColor(color("#333333"), for: .normal)
        self.plusButton.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .regular)
        self.plusButton.addTarget(self, action: #selector(plusAction), for: .touchUpInside)
        
        self.leftLine.isHidden = true
        self.leftLine.backgroundColor = color("#D7DCE5")
        self.addSubview(self.leftLine)
        self.rightLine.isHidden = true
        self.rightLine.backgroundColor = color("#D7DCE5")
        self.addSubview(self.rightLine)
        
        self.numberInputView.text = String.init(format: "%zd", self.showNumber)
        self.numberInputView.textColor = color("#333333")
        self.numberInputView.font = UIFont.systemFont(ofSize: 14, weight: .regular)
        self.numberInputView.textAlignment = .center
        self.numberInputView.delegate = self
        self.numberInputView.isEnabled = false
        
        self.addSubview(self.minusButton)
        self.addSubview(self.plusButton)
        self.addSubview(self.numberInputView)
        
    }
    
    func refreshConstraints() {
        self.minusButton.snp.makeConstraints { make in
            make.left.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(self.minusButton.snp.height).multipliedBy(1)
        }
        
        self.plusButton.snp.makeConstraints { make in
            make.right.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(self.minusButton.snp.height).multipliedBy(1)
        }
        
        self.leftLine.snp.makeConstraints { make in
            make.left.equalTo(self.minusButton.snp.right)
            make.top.bottom.equalTo(self)
            make.width.equalTo(1)
        }
        
        self.rightLine.snp.makeConstraints { make in
            make.right.equalTo(self.plusButton.snp.left)
            make.top.bottom.equalTo(self)
            make.width.equalTo(1)
        }
        
        self.numberInputView.snp.makeConstraints { make in
//            make.left.equalTo(self.minusButton.snp.right)
//            make.right.equalTo(self.plusButton.snp.left)
            make.left.equalTo(self.leftLine.snp.right)
            make.right.equalTo(self.rightLine.snp.left)
            make.top.bottom.equalTo(self)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension JDNumberInputView: UITextFieldDelegate {
    
}

extension JDNumberInputView {
    
    @objc func plusAction() {
        printLog("加号方法")
        
        self.showNumber += 1
        self.numberInputView.text = String.init(format: "%zd", self.showNumber)
        self.plusActionBlock?(self.showNumber)
    }
    
    @objc func minusAction() {
        printLog("减号方法")
        self.showNumber -= 1
        if self.showNumber < 1 {
            self.showNumber = 1
        }
        
        self.numberInputView.text = String.init(format: "%zd", self.showNumber)
        self.minusActionBlock?(self.showNumber)
    }
    
    func changeInputState() {
        self.numberInputView.isEnabled = self.isCanInput
    }
    
    // 添加边框
    func addborder() {
        self.layer.borderWidth = 1
        self.layer.borderColor = color("#D7DCE5").cgColor
        
        self.leftLine.isHidden = false
        self.rightLine.isHidden = false
        
//        self.plusButton.layer.borderWidth = 1
//        self.plusButton.layer.borderColor = color("#D7DCE5").cgColor
//
//        self.minusButton.layer.borderWidth = 1
//        self.minusButton.layer.borderColor = color("#D7DCE5").cgColor
//
//        self.numberInputView.layer.borderWidth = 1
//        self.numberInputView.layer.borderColor = color("#D7DCE5").cgColor
    }
}
